// pages/index/sureAgain.js

const App = getApp();
const navigationBarHeight = (App.statusBarHeight + 44) + 'px'
var config = require('../../utils/config');
var net = require('../../utils/net.js');
var util = require('../../utils/util.js');
import { String } from '../../utils/util.js';

Page({

  /**
   * 页面的初始数据
   */
  data: {
    navigationBarTitle: '予約確認',
    navigationBarHeight,


    noTedian:true,
    model1:{},
    model2:{},
    selectItem:{},
    dateStr:'',
    userName:'',
    userPhone:'',
    userBeizhu:'',
  
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    new App.ToastPannel();
    this.shopId = options.shopId;
    this.requestShopInfo()
    var model2 = JSON.parse(options.model2);
    this.orderId = options.orderId

    this.setData({
      userName:App.globalData.orderUserName,
      userPhone:App.globalData.orderUserPhone,
      userBeizhu: App.globalData.orderBeizhu
    })

    this.setData({
      model2:model2,
    })
    var date = model2.date

    var arr = date.split('-');
    var dateStr = date
    if (arr.length > 0) {
      dateStr = arr[0] + '年' + arr[1] + '月' + arr[2] + '日';
      this.setData({
        dateStr:dateStr
      }) 
    }
    if (!this.isBlank(App.globalData.selectIndex)){
      console.log('有item');
      this.setData({
        selectItem: App.globalData.selectIndex,
        noTedian:false
      })
    } 
  },

  // 店铺详情
  requestShopInfo:function(){
    var url = config.service.businessDetailsUrl;
    var params = {
      merchantId: this.shopId,
      latitude: App.globalData.lat,
      longitude: App.globalData.lon
    }
    net.doRequest(url, params, 'GET', true, net.Token, res => {
      if (res.code == 0) {
        var model = res.data
        if (model.distance > 1000) {
          model.distance = (model.distance / 1000.0).toFixed(1) + 'km'
        } else {
          var num = model.distance;
          model.distance = num.toFixed(1) + 'm';
        }
        var timeString = ''
        if (!util.isNull(model.operatedBegin1)) {
          console.log('转化时间')
          model.operatedBegin1 = util.zhuanHuaTime(model.operatedBegin1, 'h:m')
          model.operatedEnd1 = util.zhuanHuaTime(model.operatedEnd1, 'h:m')
          timeString = model.operatedBegin1 + '-' + model.operatedEnd1

        }
        if (!util.isNull(model.operatedBegin2)) {
          model.operatedBegin2 = util.zhuanHuaTime(model.operatedBegin2, 'h:m')
          model.operatedEnd2 = util.zhuanHuaTime(model.operatedEnd2, 'h:m')
          timeString = timeString + ' ' + model.operatedBegin2 + '-' + model.operatedEnd2

        }
        if (!util.isNull(model.operatedBegin3)) {
          model.operatedBegin3 = util.zhuanHuaTime(model.operatedBegin3, 'h:m')
          model.operatedEnd3 = util.zhuanHuaTime(model.operatedEnd3, 'h:m')
          timeString = timeString + ' ' + model.operatedBegin3 + '-' + model.operatedEnd3
        }
        model.timeString = timeString

        if (!util.isBlank(model.shopImg1)) {
          var url1 = config.service.picUrl + model.shopImg1
          model.shopImg1 = url1;
        }
        this.setData({
          model1: model,
        })
      } else {
        this.show(res.message);
      }
    })

  },

  
  submitClick:function(e){
    App.globalData.formId_3 = e.detail.formId;

  
    if (this.orderId != '空'){
      console.log('更改order')
      //更改order
      this.upDateOrder();
      return;
    }
    var url = config.service.addOrderUrl;
    var time = this.data.model2.date + ' ' + this.data.model2.time + ':00';
   

    var params = {
      discountId: this.data.selectItem.id,
      merchantId:this.data.model1.id,
      orderDate: this.data.model2.date,
      orderNum:this.data.model2.peopleCount,
      orderTime:time,
      formIds: [App.globalData.formId, App.globalData.formId_2, App.globalData.formId_3],
      phone:this.data.userPhone,
      name:this.data.userName,
      note:this.data.userBeizhu
    }

    console.log('预定参数',params);
    net.doRequest(url, params, 'POST', true, net.Token, res => {

      if (res.code == 0) {
        wx.navigateTo({
          url: './yudingSuccess',
        })
      } else {
        this.show(res.message);
      }
    })  
  },


  upDateOrder:function(){
    var url = config.service.editUrl;
    var time = this.data.model2.date + ' ' + this.data.model2.time + ':00';
    var params = {
      discountId: this.data.selectItem.id,
      orderId: this.orderId,
      orderDate: this.data.model2.date,
      orderNum: this.data.model2.peopleCount,
      orderTime: time,
      phone:this.data.userPhone,
      name:this.data.userName,
      note: this.data.userBeizhu
    }
    net.doRequest(url, params, 'POST', true, net.Token, res => {

      if (res.code == 0) {
        wx.navigateTo({
          url: '../preview/preSuccess',
        })
      } else {
        this.show(res.message);
      }
    })  
  },

  /**判断是否为空 */
  isBlank: function (str) {
    if (Object.prototype.toString.call(str) === '[object Undefined]') {//空
      return true
    } else if (
      Object.prototype.toString.call(str) === '[object String]' ||
      Object.prototype.toString.call(str) === '[object Array]') { //字条串或数组
      return str.length == 0 ? true : false
    } else if (Object.prototype.toString.call(str) === '[object Object]') {
      return JSON.stringify(str) == '{}' ? true : false
    } else {
      return true
    }
  }
})